import { setPageData, saveData, delData } from '../common-mock-method'

const pageData = {
    list: [
        {
            id: 3,
            remarks: '',
            paramName: '系统名字',
            paramKey: 'systemName',
            paramType: 1,
            paramValueName: '后台管理系统',
            paramValue: 'system'
        }
    ]
}
const pageDataDict = {
    list: [
        {
            id: 1,
            remarks: '用于系统管理-菜单与权限 模块',
            paramName: '权限类型',
            paramKey: 'permissionType',
            paramType: 2,
            paramValueName: '菜单',
            paramValue: 'MENU'
        },
        {
            id: 2,
            remarks: '用于系统管理-菜单与权限 模块',
            paramName: '权限类型',
            paramKey: 'permissionType',
            paramType: 2,
            paramValueName: '按钮',
            paramValue: 'BUTTON'
        }
    ]
}
module.exports = [
    { // page 参数
        url: '/mock/sys/params/page',
        method: 'post',
        response: (config) => {
            return setPageData(pageData, config, 'username', 'nickName')
        }
    },
    { // page 系统字典
        url: '/mock/sys/params/dict/page',
        method: 'post',
        response: (config) => {
            const requestBody = config.body
            const result = {total: 0, list: []}
            pageDataDict.list.map(item => {
                let count = []
                if (result.list.length) {
                    count = result.list.filter(({paramKey}) => paramKey === item.paramKey)
                }
                if ( !count.length) result.list.push(item)
            })
            if (requestBody.keyword) {
                result.list = result.list.filter(item => item.paramName.indexOf(requestBody.keyword) > -1)
            }
            result.total = result.list.length
            result.list = result.list.slice((requestBody.pageNum - 1) * requestBody.pageSize, (requestBody.pageNum) * requestBody.pageSize)
            return {
                code: 200,
                data: result,
                message: 'OK'
            }
        }
    },
    { // 根据字典键(paramKey)获取list
        url: '/mock/sys/params/dict/list',
        method: 'post',
        response: (config) => {
            const _result = pageDataDict.list.filter(item => item.paramKey.indexOf(config.body.paramKey) > -1)
            return {
                code: 200,
                data: _result,
                message: 'OK'
            }
        }
    },
    {// 保存
        url: '/mock/sys/params/save',
        method: 'post',
        response: (config) => {
            if (config.body.paramType === 1) { // 1:参数 2:字典
                return saveData(config, pageData)
            } else if (config.body.paramType === 2) {
                return saveData(config, pageDataDict)
            }
        }
    },
    {// 删除 参数
        url: RegExp('/mock/sys/params/del/' + '.*'),
        method: 'post',
        response: (config) => {
            return delData(config, '/mock/sys/params/del/', pageData)
        }
    },
    {// 删除 系统字典
        url: RegExp('/mock/sys/params/dict/del/' + '.*'),
        method: 'post',
        response: (config) => {
            return delData(config, '/mock/sys/params/dict/del/', pageDataDict, [1, 2])
        }
    }
]
